//
//  My_LiShuZuZhi.m
//  HappyStore
//
//  Created by 点点充 on 16/1/27.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "My_LiShuZuZhi.h"

@interface My_LiShuZuZhi ()
{
    NSMutableArray *muAryData;
    NSInteger paggge;
    
    NSMutableDictionary *muDicParameters;
}
@end

@implementation My_LiShuZuZhi

- (void)viewDidLoad {
    [super viewDidLoad];
    muAryData = [NSMutableArray array];
    paggge = 1;
    muDicParameters = [NSMutableDictionary dictionary];
    
    _mainTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (muAryData.count != 0) {
            paggge = 1;
            [muDicParameters setObject:@"1" forKey:@"page"];
            [self startSearch];
        }else {
            [_mainTableView.mj_header endRefreshing];
        }
    }];
    
    
    
    _mainTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        paggge += 1;
        [muDicParameters setObject:[NSString stringWithFormat:@"%ld",paggge] forKey:@"page"];
        [self startSearch];
    }];
    
    _mainTableView.mj_footer.hidden = YES;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    if (muAryData.count != 0) {
        muAryData = [NSMutableArray array];
        [_mainTableView reloadData];
    }
    return YES;
}

- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    NSLog(@"%@",searchBar.text);
    
    if (searchBar.text.length == 0) {
        return YES;
    }else {//token=rE3F2m8AvfC7CVh85O9q&name=模糊字段&userId=用户id&userType=用户类型
        paggge = 1;
        muDicParameters = [NSMutableDictionary dictionary];
        [muDicParameters setObject:[USER_DEFAULTS objectForKey:@"usertype"] forKey:@"userType"];//用户类型 1-个人，2-企业，3-政府
        [muDicParameters setObject:[USER_DEFAULTS objectForKey:@"userid"] forKey:@"userId"];
        [muDicParameters setObject:[USER_DEFAULTS objectForKey:@"token"] forKey:@"token"];//验证用户
        [muDicParameters setObject:[NSString stringWithFormat:@"%ld",paggge] forKey:@"page"];//页码
        [muDicParameters setObject:searchBar.text forKey:@"name"];//模糊字段
        [self startSearch];
        return YES;
    }
    
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"-----%@",searchBar.text);
    if (searchBar.text.length != 0) {
        [searchBar resignFirstResponder];
        paggge = 1;
        muDicParameters = [NSMutableDictionary dictionary];
        [muDicParameters setObject:[USER_DEFAULTS objectForKey:@"usertype"] forKey:@"userType"];//用户类型 1-个人，2-企业，3-政府
        [muDicParameters setObject:[USER_DEFAULTS objectForKey:@"userid"] forKey:@"userId"];
        [muDicParameters setObject:[USER_DEFAULTS objectForKey:@"token"] forKey:@"token"];//验证用户
        [muDicParameters setObject:[NSString stringWithFormat:@"%ld",paggge] forKey:@"page"];//页码
        [muDicParameters setObject:searchBar.text forKey:@"name"];//模糊字段
        [self startSearch];
    }
    
    
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return muAryData.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellSingle = @"cellSingle";//首页
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellSingle forIndexPath:indexPath];
    
    UILabel *labTitle = [cell viewWithTag:10];
    if (muAryData[indexPath.row][@"username"] == [NSNull null] || [muAryData[indexPath.row][@"username"] isEqualToString:@"null"]) {
        labTitle.text = muAryData[indexPath.row][@"account"];
    }else {
        labTitle.text = [NSString stringWithFormat:@"%@(%@)",muAryData[indexPath.row][@"username"],muAryData[indexPath.row][@"account"]];
    }
    
    
    
    
    
    
    
    return cell;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];//取消选中状态
    
    //取消显示后退键文字
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = barButtonItem;
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"setLiShuZuzhi" object:nil userInfo:muAryData[indexPath.row]];
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)startSearch
{
    //token=rE3F2m8AvfC7CVh85O9q&name=模糊字段&userId=用户id&userType=用户类型
    [self showRoundProgressWithTitle:@"请稍后..."];
    
    NSString *strURl = [NSString stringWithFormat:@"%@%@",URL_DEFINE,@"membershipUser_app_myHome.im.lensum"];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:strURl parameters:muDicParameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dicInfo = [NSDictionary dictionaryWithDictionary:responseObject];
//        NSLog(@"%@",dicInfo);
        
        if ([[dicInfo objectForKey:@"result"] integerValue] == 1) {
            
            if (paggge == 1) {
                [_mainTableView.mj_header endRefreshing];
                
                muAryData = [NSMutableArray arrayWithArray:[dicInfo objectForKey:@"data"]];
                
                if (muAryData.count == 10) {
                    _mainTableView.mj_footer.hidden = NO;
                }else {
                    _mainTableView.mj_footer.hidden = YES;
                }
                
            }else {
                [_mainTableView.mj_footer endRefreshing];
                NSArray *ary11 = [dicInfo objectForKey:@"data"];
                for (int i = 0; i<ary11.count; i++) {
                    NSDictionary *dic1 = ary11[i];
                    [muAryData addObject:dic1];
                }
                if (ary11.count == 10) {
                    _mainTableView.mj_footer.hidden = NO;
                }else {
                    _mainTableView.mj_footer.hidden = YES;
                }
                
            }
            
            
            [_mainTableView reloadData];
            [self hideBubble];
        }else {
            [_mainTableView reloadData];
            [_mainTableView.mj_header endRefreshing];
            [_mainTableView.mj_footer endRefreshing];

            [self showSingleIconBubble:[dicInfo objectForKey:@"msg"]];

        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [_mainTableView.mj_header endRefreshing];
        [_mainTableView.mj_footer endRefreshing];

        [self showSingleIconBubble:error.localizedDescription];

    }];
}


@end
